package com.web.dao;

import com.web.controller.web.WebOrderController;
import com.web.entity.Order;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * @author FDjavaone
 */
public class OrderSqlProvider {


    public String insertSelective(Order record) {
        BEGIN();
        INSERT_INTO("t_order");

        if (record.getmId() != null) {
            VALUES("m_id", "#{mId,jdbcType=BIGINT}");
        }

        if (record.getStoreId() != null) {
            VALUES("store_id", "#{storeId,jdbcType=BIGINT}");
        }

        if (record.getOrderNum() != null) {
            VALUES("order_num", "#{orderNum,jdbcType=VARCHAR}");
        }

        if (record.getOrderType() != null) {
            VALUES("order_type", "#{orderType,jdbcType=VARCHAR}");
        }

        if (record.getExpressName() != null) {
            VALUES("express_name", "#{expressName,jdbcType=VARCHAR}");
        }

        if (record.getExpressNumber() != null) {
            VALUES("express_number", "#{expressNumber,jdbcType=VARCHAR}");
        }

        if (record.getName() != null) {
            VALUES("name", "#{name,jdbcType=VARCHAR}");
        }

        if (record.getPhone() != null) {
            VALUES("phone", "#{phone,jdbcType=VARCHAR}");
        }

        if (record.getAddress() != null) {
            VALUES("address", "#{address,jdbcType=VARCHAR}");
        }

        if (record.getTotalPrice() != null) {
            VALUES("total_price", "#{totalPrice,jdbcType=DECIMAL}");
        }

        if (record.getTipPrice() != null) {
            VALUES("tip_price", "#{tipPrice,jdbcType=DECIMAL}");
        }

        if (record.getTransferPrice() != null) {
            VALUES("transfer_price", "#{transferPrice,jdbcType=DECIMAL}");
        }

        if (record.getStatus() != null) {
            VALUES("status", "#{status,jdbcType=INTEGER}");
        }

        if (record.getPayType() != null) {
            VALUES("pay_type", "#{payType,jdbcType=INTEGER}");
        }

        if (record.getCreateTime() != null) {
            VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getModifyTime() != null) {
            VALUES("modify_time", "#{modifyTime,jdbcType=TIMESTAMP}");
        }

        return SQL();
    }


    public String updateByPrimaryKeySelective(Order record) {
        BEGIN();
        UPDATE("t_order");

        if (record.getmId() != null) {
            SET("m_id = #{mId,jdbcType=BIGINT}");
        }

        if (record.getStoreId() != null) {
            SET("store_id = #{storeId,jdbcType=BIGINT}");
        }

        if (record.getOrderNum() != null) {
            SET("order_num = #{orderNum,jdbcType=VARCHAR}");
        }

        if (record.getOrderType() != null) {
            SET("order_type = #{orderType,jdbcType=VARCHAR}");
        }

        if (record.getExpressName() != null) {
            SET("express_name = #{expressName,jdbcType=VARCHAR}");
        }

        if (record.getExpressNumber() != null) {
            SET("express_number = #{expressNumber,jdbcType=VARCHAR}");
        }

        if (record.getName() != null) {
            SET("name = #{name,jdbcType=VARCHAR}");
        }

        if (record.getPhone() != null) {
            SET("phone = #{phone,jdbcType=VARCHAR}");
        }

        if (record.getAddress() != null) {
            SET("address = #{address,jdbcType=VARCHAR}");
        }

        if (record.getTotalPrice() != null) {
            SET("total_price = #{totalPrice,jdbcType=DECIMAL}");
        }

        if (record.getTipPrice() != null) {
            SET("tip_price = #{tipPrice,jdbcType=DECIMAL}");
        }

        if (record.getTransferPrice() != null) {
            SET("transfer_price=#{transferPrice,jdbcType=DECIMAL}");
        }

        if (record.getStatus() != null) {
            SET("status = #{status,jdbcType=INTEGER}");
        }

        if (record.getPayType() != null) {
            SET("pay_type = #{payType,jdbcType=INTEGER}");
        }

        if (record.getCreateTime() != null) {
            SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getModifyTime() != null) {
            SET("modify_time = #{modifyTime,jdbcType=TIMESTAMP}");
        }

        WHERE("id = #{id,jdbcType=BIGINT}");

        return SQL();
    }


    public String selectByOrder(Order record) {
        BEGIN();
        SELECT("id");
        SELECT("m_id");
        SELECT("store_id");
        SELECT("order_num");
        SELECT("order_type");
        SELECT("express_name");
        SELECT("express_number");
        SELECT("name");
        SELECT("phone");
        SELECT("address");
        SELECT("total_price");
        SELECT("tip_price");
        SELECT("transfer_price");
        SELECT("status");
        SELECT("pay_type");
        SELECT("create_time");
        SELECT("modify_time");
        FROM("t_order");
        if (record.getmId() != null) {
            WHERE("m_id = #{mId,jdbcType=BIGINT}");
        }

        if (record.getStoreId() != null) {
            WHERE("store_id = #{storeId,jdbcType=BIGINT}");
        }

        if (record.getOrderNum() != null) {
            WHERE("order_num = #{orderNum,jdbcType=VARCHAR}");
        }

        if (record.getOrderType() != null) {
            WHERE("order_type = #{orderType,jdbcType=VARCHAR}");
        }

        if (record.getExpressName() != null) {
            WHERE("express_name = #{expressName,jdbcType=VARCHAR}");
        }

        if (record.getExpressNumber() != null) {
            WHERE("express_number = #{expressNumber,jdbcType=VARCHAR}");
        }

        if (record.getName() != null) {
            WHERE("name = #{name,jdbcType=VARCHAR}");
        }

        if (record.getPhone() != null) {
            WHERE("phone = #{phone,jdbcType=VARCHAR}");
        }

        if (record.getAddress() != null) {
            WHERE("address = #{address,jdbcType=VARCHAR}");
        }

        if (record.getTotalPrice() != null) {
            WHERE("total_price = #{totalPrice,jdbcType=DECIMAL}");
        }

        if (record.getTipPrice() != null) {
            WHERE("tip_price = #{tipPrice,jdbcType=DECIMAL}");
        }

        if (record.getTransferPrice() != null) {
            WHERE("transfer_price = #{transferPrice,jdbcType=DECIMAL}");
        }

        if (record.getStatus() != null) {
            WHERE("status = #{status,jdbcType=INTEGER}");
        }

        if (record.getPayType() != null) {
            WHERE("pay_type = #{payType,jdbcType=INTEGER}");
        }

        if (record.getCreateTime() != null) {
            WHERE("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getModifyTime() != null) {
            WHERE("modify_time = #{modifyTime,jdbcType=TIMESTAMP}");
        }

        if (record.getId() != null) {
            WHERE("id = #{id,jdbcType=BIGINT}");
        }
        return SQL();
    }

    public String selectAllOrders(WebOrderController.OrderColumns record) {
        BEGIN();
        SELECT("id");
        SELECT("m_id");
        SELECT("order_num");
        SELECT("store_id");
        SELECT("order_type");
        SELECT("express_name");
        SELECT("express_number");
        SELECT("name");
        SELECT("phone");
        SELECT("address");
        SELECT("total_price");
        SELECT("tip_price");
        SELECT("transfer_price");
        SELECT("status");
        SELECT("pay_type");
        SELECT("create_time");
        SELECT("modify_time");
        FROM("t_order");

        if (record.getOrderNum() != null && !"".equals(record.getOrderNum())) {
            WHERE("order_num = #{orderNum,jdbcType=VARCHAR}");
        }

        if (record.getOrderType() != null && !"".equals(record.getOrderType())) {
            WHERE("order_type = #{orderType,jdbcType=VARCHAR}");
        }

        if (record.getExpressNumber() != null && !"".equals(record.getExpressNumber())) {
            WHERE("express_number = #{expressNumber,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null && !"".equals(record.getStatus())) {
            WHERE("status = #{status,jdbcType=INTEGER}");
        }

        if(record.getBegin_createTime()!=null){
            WHERE("create_time >= #{begin_createTime,jdbcType=TIMESTAMP}");
        }
        if(record.getEnd_createTime()!=null){
            WHERE("create_time <= #{end_createTime,jdbcType=TIMESTAMP}");
        }
        if(record.getBegin_modifyTime()!=null){
            WHERE("modify_time >= #{begin_modifyTime,jdbcType=TIMESTAMP}");
        }
        if(record.getEnd_modifyTime()!=null){
            WHERE("modify_time >= #{end_modifyTime,jdbcType=TIMESTAMP}");
        }

        ORDER_BY("create_time desc");

        return SQL();
    }


}